<template>
  <el-form class="tl-form" ref="form" label-width="150px" style="background: white;box-shadow: 0 0 10px #ccc;width: var(--tableWidth);margin: 10px 0 10px 10px" v-show="canSeenForm">
    <div style="padding: 10px">
    <div class="tl-form-title tl-form-line-title2">
      <el-row>
        <el-col :span="6">任务反馈</el-col>
        <el-col :span="18" style="float: right">
          <el-button type="text" icon="el-icon-circle-plus" size="medium" @click="addCloseDetail" style="float: right" v-if="canAddTaskClose">新增异常关闭</el-button>
          <el-button type="text" icon="el-icon-circle-plus" size="medium" @click="addFeedBackDetail" style="float: right" v-if="canAddTaskFeedBack">新增任务反馈</el-button>
          <el-button type="text" icon="el-icon-circle-plus" size="medium" @click="addGapDetail" style="float: right" v-if="canEditGap">新增差距与改进</el-button>
          <el-button type="text" icon="el-icon-circle-plus" size="medium" @click="addStartDetail" style="float: right" v-if="canStartTask">开始任务</el-button>
        </el-col>
      </el-row>
    </div>
    </div>
    <el-timeline>
      <el-timeline-item v-for="(item, index) in feedbackList" :key="index" color="#0A6ED2" style="padding-bottom: 25px">
        <div v-if="item.istaskclose === '1'">
          <tl-close-expand :ref="'close' + index" :newValue="item" @refresh="queryTaskFeedbackList(id)" @deleteList="deleteList(index)"></tl-close-expand>
        </div>
        <div v-if="item.isstarttask === '1'">
          <tl-start-expand :ref="'start' + index" :newValue="item" @refresh="changeTaskState"></tl-start-expand>
        </div>
        <div v-if="item.istaskfeedback === '1'">
          <tl-edit-expand :ref="'feedback' + index" :newValue="item" @refresh="queryTaskFeedbackList(id)" :canEdit="canEditFeedback" @deleteList="deleteList(index)" :taskInfo="taskInfo"></tl-edit-expand>
        </div>
        <div v-if="item.istaskfeedback === '0'">
          <tl-gap-expand :ref="'gap' + index" :newValue="item" :canEdit="canEditGap" @refresh="queryTaskFeedbackList(id)" @deleteList="deleteList(index)"></tl-gap-expand>
        </div>
      </el-timeline-item>
      <el-button type="text" @click="doChangeFeedBackList" v-if="showChangeButton" size="medium">{{ buttontext }}</el-button>
      <span v-if="buttonflag && showChangeButton" style="color: #888">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;共{{ totalCount }}条</span>
    </el-timeline>
  </el-form>
</template>

<script>
/** mixins */
import { mixin_edit } from '@tapui/mixins/mixin_edit'
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
import { formatDate } from '@tapui/utils/date'
import { validateNull } from '@tapui/utils/validate'
import tlEditExpand from '@/views/mpm/mpmProjectTaskFeedback/editExpand'
import tlGapExpand from '@/views/mpm/mpmProjectTaskFeedback/managerEditExpand'
import tlStartExpand from '@/views/mpm/mpmProjectTaskFeedback/startTaskEditExpand'
import tlCloseExpand from '@/views/mpm/mpmProjectTaskFeedback/closeTaskExpand'
/** VUE配置 */
export default {
  mixins: [mixin_edit],
  components: { tlEditExpand, tlGapExpand, tlStartExpand, tlCloseExpand },
  data() {
    return {
      urlPrefix: '/mpm/projectTaskFeedback/',
      url: {
        allList: '/mpm/projectTaskFeedback/allList',
        queryCloseList: '/mpm/projectTaskClose/list'
      },
      feedbackList: [],
      feedbackList2: [],
      tempfeedbackList: [],
      buttonflag: true,
      totalCount: 0,
      taskId: 0,
      queryParam: {},
      pickerOptions: {
        disabledDate: (time) => {
          let now = new Date()
          now.setHours(0, 0, 0, 0)
          return time.getTime() > now.getTime()
        }
      }
    }
  },
  props: {
    taskInfo: {
      type: Object,
      default: () => {}
    }
  },
  computed: {
    showChangeButton() {
      return this.feedbackList2.length > 0
    },
    buttontext() {
      return this.buttonflag ? '查看更多任务反馈' : '隐藏'
    },
    isPM() {
      if (this.$store.getters.projectroles) {
        return this.$store.getters.projectroles.indexOf('pm') > -1
      } else {
        return false
      }
    },
    // 能够新增任务反馈
    canAddTaskFeedBack() {
      return this.taskInfo.responsiblecode === this.user.code && this.taskInfo.taskstate === '1' && this.feedbackList.filter((item) => item.istaskfeedback === '1' && (item.state === 0 || item.state === 2 || item.taskschedule === 100)).length === 0 && this.feedbackList2.filter((item) => item.istaskfeedback === '1' && (item.state === 0 || item.state === 2 || item.taskschedule === 100)).length === 0
    },
    // 可以编辑任务反馈
    canEditFeedback() {
      return this.taskInfo.responsiblecode === this.user.code && this.taskInfo.taskstate === '1'
    },
    // 可以新增、编辑差距与改进
    canEditGap() {
      return this.taskInfo.responsiblecode === this.user.code || this.isPM
    },
    // 可以新增开始任务
    canStartTask() {
      return this.taskInfo.responsiblecode === this.user.code && this.taskInfo.taskstate === '0' && this.taskInfo.state === 9
    },
    // 可以看到form表单
    canSeenForm() {
      return this.feedbackList.length > 0 || this.canAddTaskFeedBack || this.canEditGap || this.canStartTask || this.canAddTaskClose
    },
    // 可以新增异常关闭
    canAddTaskClose() {
      return this.taskInfo.responsiblecode === this.user.code && this.taskInfo.taskstate === '1' && this.feedbackList.filter((item) => item.istaskclose === '1').length === 0
    }
  },
  methods: {
    // 重写open方法，调用查找任务反馈的方法
    open() {
      this.model = {}
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
        if (this.isAdd === true) {
          this.title = '添加' + this.moduleTitle
          // 需要添加重复判断
          if (this.$store.getters.param.isAvoidRepeat === true) {
            this.queryBillUid()
          }
          if (this.isQueryInitData === true) {
            // 后台进行数据初始化
            this.queryInitData().then((_) => {
              this.assignDefaultValue()
              this.$refs.form.clearValidate()
              this.loaded()
            })
          } else {
            // 前台进行数据初始化
            this.$set(this.model, 'enabled', 1)
            this.$set(this.model, 'sortcode', 0)
            this.assignDefaultValue()
            this.$refs.form.clearValidate()
            this.loaded()
          }
        } else if (this.disableEditSubmit === false) {
          this.title = '编辑' + this.moduleTitle
          this.queryTaskFeedbackList(this.id).then((_) => {
            this.loaded()
          })
        } else {
          this.title = '查看' + this.moduleTitle
          this.queryTaskFeedbackList(this.id).then((_) => {
            this.$refs.form.clearValidate()
            this.loaded()
          })
        }
      })
    },
    queryTaskFeedbackList(taskId) {
      // 初始化data值
      this.taskId = taskId
      this.feedbackList = []
      this.feedbackList2 = []
      this.buttonflag = true
      this.totalCount = 0
      let param = {
        qry_taskid: taskId,
        sidx: 'submitdate',
        sord: 'desc',
        currentPage: 1,
        pageSize: 50
      }
      // 如果不是项目经理和任务责任人，只能看到审批中和审批完成的任务反馈信息
      if (!this.isPM && this.taskInfo.responsiblecode !== this.user.code) {
        Object.assign(param, { qry_state: '1,9' })
      }
      return getAction(this.url.allList, param)
        .then((res) => {
          if (res.success) {
            if (res.result.length > 0) {
              if (res.result.length > 3) {
                this.feedbackList = res.result.slice(0, 3)
                this.feedbackList2 = res.result
              } else {
                this.feedbackList = res.result
              }
              this.totalCount = res.result.length
              this.$emit('resize')
            }
          } else {
            this.$tap.alert(res.message, 'warning')
          }
        })
        .catch((err) => {
          this.$tap.alert(err.message, 'warning')
        })
    },
    formatDate(date) {
      return formatDate(new Date(date ? date.toString().replace(/\-/g, "/") : date), "yyyy-MM-dd HH:mm")
    },
    doChangeFeedBackList() {
      this.tempfeedbackList = this.feedbackList
      this.feedbackList = this.feedbackList2
      this.feedbackList2 = this.tempfeedbackList
      this.buttonflag = !this.buttonflag
    },
    addFeedBackDetail() {
      let feedbackvalue = this.assignFeedBackDefaultValue()
      this.feedbackList.unshift(feedbackvalue)
    },
    assignFeedBackDefaultValue() {
      let feedbackvalue = {}
      if (this.codeModel.flowidList && this.codeModel.flowidList.length > 0) {
        this.$set(feedbackvalue, 'flowid', parseInt(this.codeModel.flowidList[0].id))
      }
      this.$set(feedbackvalue, 'applyorgcode', this.org.code)
      this.$set(feedbackvalue, 'applyorgname', this.org.name)
      this.$set(feedbackvalue, 'applydeptcode', this.dept.code)
      this.$set(feedbackvalue, 'applydeptname', this.dept.fullname)
      this.$set(feedbackvalue, 'applyusercode', this.user.code)
      this.$set(feedbackvalue, 'applyusername', this.user.name)
      this.$set(feedbackvalue, 'usercode', this.user.code)
      this.$set(feedbackvalue, 'username', this.user.name)
      this.$set(feedbackvalue, 'deptcode', this.dept.code)
      this.$set(feedbackvalue, 'orgcode', this.org.code)
      this.$set(feedbackvalue, 'feedbackdate', new Date())
      this.$set(feedbackvalue, 'submitdate', new Date())
      this.$set(feedbackvalue, 'taskid', this.taskInfo.id)
      this.$set(feedbackvalue, 'taskname', this.taskInfo.taskname)
      this.$set(feedbackvalue, 'taskschedule', this.taskInfo.taskschedule)
      this.$set(feedbackvalue, 'taskstate', this.taskInfo.taskstate)
      this.$set(feedbackvalue, 'planstartdate', this.taskInfo.planstartdate.substring(0, 10))
      this.$set(feedbackvalue, 'planenddate', this.taskInfo.planenddate.substring(0, 10))
      this.$set(feedbackvalue, 'taskdistributedate', this.taskInfo.taskdistributedate.substring(0, 10))
      this.$set(feedbackvalue, 'projectcode', this.taskInfo.projectcode)
      this.$set(feedbackvalue, 'taskactualstartdate', this.taskInfo.actualstartdate)
      this.$set(feedbackvalue, 'taskdetail', this.taskInfo.taskdetail)
      this.$set(feedbackvalue, 'customerattach', this.taskInfo.customerattach)
      this.$set(feedbackvalue, 'improveplan', this.taskInfo.improveplan)
      this.$set(feedbackvalue, 'state', 0)
      this.$set(feedbackvalue, 'istaskfeedback', '1')
      if (this.taskInfo.dmdtask === 1) {
        this.$set(feedbackvalue, 'havenamelist', this.taskInfo.havenamelist)
      }
      if (this.taskInfo.dmdtask === 2) {
        this.$set(feedbackvalue, 'guoscore', this.taskInfo.guoscore)
        this.$set(feedbackvalue, 'guorank', this.taskInfo.guorank)
        this.$set(feedbackvalue, 'guoposition', this.taskInfo.guoposition)
      }
      if (this.taskInfo.dmdtask === 3) {
        this.$set(feedbackvalue, 'reviewmeetname', this.taskInfo.reviewmeetname)
        this.$set(feedbackvalue, 'reviewmeetingdocument', this.taskInfo.reviewmeetingdocument)
      }
      if (this.taskInfo.dmdtask === 4) {
        this.$set(feedbackvalue, 'nankeepscore', this.taskInfo.nankeepscore)
        this.$set(feedbackvalue, 'nankeeprank', this.taskInfo.nankeeprank)
        this.$set(feedbackvalue, 'nanmovescore', this.taskInfo.nanmovescore)
        this.$set(feedbackvalue, 'nanmoverank', this.taskInfo.nanmoverank)
      }
      return feedbackvalue
    },
    addGapDetail() {
      let gapvalue = this.assignGapDefaultValue()
      this.feedbackList.unshift(gapvalue)
    },
    assignGapDefaultValue() {
      let gapvalue = {}
      this.$set(gapvalue, 'applyorgcode', this.org.code)
      this.$set(gapvalue, 'applyorgname', this.org.name)
      this.$set(gapvalue, 'applydeptcode', this.dept.code)
      this.$set(gapvalue, 'applydeptname', this.dept.fullname)
      this.$set(gapvalue, 'applyusercode', this.user.code)
      this.$set(gapvalue, 'applyusername', this.user.name)
      this.$set(gapvalue, 'usercode', this.user.code)
      this.$set(gapvalue, 'username', this.user.name)
      this.$set(gapvalue, 'deptcode', this.dept.code)
      this.$set(gapvalue, 'orgcode', this.org.code)
      this.$set(gapvalue, 'feedbackdate', new Date())
      this.$set(gapvalue, 'taskid', this.taskInfo.id)
      this.$set(gapvalue, 'taskname', this.taskInfo.taskname)
      this.$set(gapvalue, 'projectcode', this.taskInfo.projectcode)
      this.$set(gapvalue, 'istaskfeedback', '0')
      this.$set(gapvalue, 'state', 9)
      this.$set(gapvalue, 'submitdate', new Date())
      return gapvalue
    },
    addStartDetail() {
      this.feedbackList.unshift({ taskid: this.taskInfo.id, isstarttask: '1' })
    },
    changeTaskState() {
      this.taskInfo.taskstate = '1'
      this.queryTaskFeedbackList(this.id)
    },
    addCloseDetail() {
      getAction(this.url.queryCloseList, { qry_taskid: this.taskInfo.id, qry_state: '0,1,2' }).then((res) => {
        if (res.success && res.result.dataList.length > 0) {
          if (res.result.dataList[0].state === 1) {
            this.$tap.alert('已提交异常关闭审批，请勿重复提交!', 'warning')
          } else {
            let temp = res.result.dataList[0]
            Object.assign(temp, { istaskclose: '1' })
            this.feedbackList.unshift(temp)
          }
        } else {
          let closevalue = this.assignCloseDefaultValue()
          this.feedbackList.unshift(closevalue)
        }
      })
    },
    assignCloseDefaultValue() {
        let closevalue = {}
        if (this.codeModel.flowidList && this.codeModel.flowidList.length > 0) {
          this.$set(closevalue, 'flowid', parseInt(this.codeModel.flowidList[0].id))
        }
        this.$set(closevalue, 'applyorgcode', this.org.code)
        this.$set(closevalue, 'applyorgname', this.org.name)
        this.$set(closevalue, 'applydeptcode', this.dept.code)
        this.$set(closevalue, 'applydeptname', this.dept.fullname)
        this.$set(closevalue, 'applyusercode', this.user.code)
        this.$set(closevalue, 'applyusername', this.user.name)
        this.$set(closevalue, 'usercode', this.user.code)
        this.$set(closevalue, 'username', this.user.name)
        this.$set(closevalue, 'deptcode', this.dept.code)
        this.$set(closevalue, 'orgcode', this.org.code)
        this.$set(closevalue, 'feedbackdate', new Date())
        this.$set(closevalue, 'taskid', this.taskInfo.id)
        this.$set(closevalue, 'taskname', this.taskInfo.taskname)
        this.$set(closevalue, 'taskschedule', this.taskInfo.taskschedule)
        this.$set(closevalue, 'taskstate', this.taskInfo.taskstate)
        this.$set(closevalue, 'planstartdate', this.taskInfo.planstartdate.substring(0, 10))
        this.$set(closevalue, 'planenddate', this.taskInfo.planenddate.substring(0, 10))
        this.$set(closevalue, 'taskdistributedate', this.taskInfo.taskdistributedate.substring(0, 10))
        this.$set(closevalue, 'projectcode', this.taskInfo.projectcode)
        this.$set(closevalue, 'taskdetail', this.taskInfo.taskdetail)
        this.$set(closevalue, 'customerattach', this.taskInfo.customerattach)
        this.$set(closevalue, 'improveplan', this.taskInfo.improveplan)
        this.$set(closevalue, 'istaskclose', '1')
        return closevalue
      },
      deleteList(index) {
        this.feedbackList.splice(index, 1)
      }
  }
}
</script>
<style scoped lang="scss">
.tl-form-title.tl-form-line-title2 {
  font-size: 18px;
  font-weight: bold;
  height: 28px;
  line-height: 28px;
  border-left: 4px solid #409eff;
  padding-left: 8px;
  overflow: hidden;
}
</style>
